package model.dao.impl;


import dto.Fecha;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import dto.config;
import model.dao.DaoPronostico;
import service.sql.Sql;

/*
 * DaoPronosticoImp
 *
 * Descripción: Esta clase DaoPronosticoImp
 *
 * Autor: Erik
 *
 * Versión: 0.1
 *
 * Fecha: 23/04/14
 *
 */
public class DaoPronosticoImp implements DaoPronostico {

    private Sql sql;

    public DaoPronosticoImp() {
        sql = new Sql();
    }



    @Override
    public List<Object[]> reporte() {
        String s =
                //"select top 40040  PAIS,SEGMENTO,GRUPOCLIENTE,REGION,DESTINO,SOCIEDAD,PRODUCTO,CODIGOSAPCLIENTE,CLIENTE FROM DIA  "
                "SELECT pr.PAIS,pr.SEGMENTO,pr.DESTINO,pr.MATERIAL_2,D.WDIA,pr.Anho, (pr.DESPACHO*POR) PRONOSTICO , pr.SEMANA,  D.POR "
                + "FROM dbo.PRONOSTICO pr "
                + "left join  "
                + "(select D.PAIS,D.SEGMENTO,D.DESTINO,D.MATERIAL,D.WDIA,(D.SUMA/SxD.SUMA) POR , D.SEMANA "
                + "from vista_DIA D left join vista_SEMANAxDIA SxD "
                + "on D.PAIS=SxD.PAIS AND D.SEGMENTO=SxD.SEGMENTO AND D.MATERIAL=SxD.MATERIAL AND D.DESTINO=SxD.DESTINO AND D.SEMANA=SxD.SEMANA) D "
                + "on D.PAIS=pr.PAIS AND D.SEGMENTO=pr.SEGMENTO AND D.MATERIAL=pr.MATERIAL_2 AND D.DESTINO=pr.DESTINO AND D.SEMANA=pr.SEMANA "
                + "order by PRONOSTICO desc";
        List<Object[]> list = sql.consulta(s);
        return list;
    }

    @Override
    public List<Object[]> reporte2(String id) {
        String s =
                 "select tabla_nombre_pronostico  from forecast_config  where idconfig="+id;
        List<Object[]> list = sql.consulta2(s, true);
        return list;
    }

    @Override
    public List<Object[]> anios() {
        String s = "select Anho from cemento_historico_dia group by Anho order by Anho";
        List<Object[]> list = sql.consulta(s, false);
        return list;
    }

    @Override
    public List<Object[]> anios2(String id) {
        String s = "select tabla_nombre_historico from forecast_config where idconfig="+id;
        List<Object[]> list = sql.consulta2(s);
      
        return list;
    }

    @Override
    public List<Object[]> colum(String id) {
        String s = "select cl.descripcioncolumna,i.descripcionitem from forecast_config_item_columna cic "
                + "left join forecast_config c on cic.idconfig=c.idconfig "
                + "left join forecast_item i on cic.iditem=i.iditem "
                + "left join forecast_columna cl on cic.idcolumna=cl.idcolumna where c.idconfig= " + id;
        List<Object[]> list = sql.consulta(s, false);
        return list;
    }

    @Override
    public String UPD_Vistas(String[] s) {
        String vistas = "  exec PROC_FORECAST_VISTAS '" + s[0] + "','" + s[6] + "',\'" + s[1] + "' ,'" + s[2] + "','" + s[4] + "','" + s[3] + "'," + s[5] + "   ";
      String result = sql.ejecuta(vistas);

        return result;
    }

    @Override
    public List<config> config() {
        String s = "select * from forecast_config ";
        List<Object[]> list = sql.consulta(s, false);
        List<config> listS= new ArrayList<config>();
        for (int i = 0; i < list.size(); i++) {			
        	 Object[] fila = (Object[])list.get(i);
        	 dto.config obj= new dto.config(Integer.parseInt(fila[0].toString()),fila[1].toString());
        	 listS.add(obj);
		}
        return listS;
    }
}
